from datetime import datetime

from rest_framework import serializers
from rest_framework.fields import HiddenField, CurrentUserDefault

from app.goods.models import Goods
from app.goods.serializers import GoodListSerializer
from app.user_operate.models import UserFav, UserLeavingMessage, UserAddress


class UserFavSerializer(serializers.ModelSerializer):
    # 隐式字段，自动填充当前用户
    user = HiddenField(default=CurrentUserDefault())

    class Meta:
        model = UserFav
        fields = ('user', 'goods', 'id')


class UserFavDetailSerializer(serializers.ModelSerializer):
    goods = GoodListSerializer()

    class Meta:
        model = UserFav
        fields = ('goods', 'id')


# 用户留言的序列化
class LeaveMessageSearializer(serializers.ModelSerializer):
    # 隐式字段，自动填充当前用户
    user = HiddenField(default=CurrentUserDefault())
    add_time = serializers.DateTimeField(read_only=True, format='%Y-%m_%d %H-%M')

    class Meta:
        model = UserLeavingMessage
        fields = ['user', 'message_type', 'subject', 'message', 'file', 'add_time', 'id']


class UserAddressSerializer(serializers.ModelSerializer):
    # 隐式字段，自动填充当前用户
    user = HiddenField(default=CurrentUserDefault())
    add_time = serializers.DateTimeField(read_only=True, format='%Y-%m_%d %H-%M')

    class Meta:
        model = UserAddress
        fields = '__all__'
